#include <iostream>
#include "../../common/detail.hpp"
#include "../../server/rpc_server.hpp"

void Divide(const Json::Value &params, Json::Value &result){
    int num1 = params["num1"].asInt();
    int num2 = params["num2"].asInt();
    result = num1 / num2;
}

void Multiply(const Json::Value &params, Json::Value &result){
    int num1 = params["num1"].asInt();
    int num2 = params["num2"].asInt();
    result = num1 * num2;
}

int main(){
    myrpc::initLoggers();

    // 注册除法服务
    std::unique_ptr<myrpc::server::SDescribeBuilder> divide_builder(new myrpc::server::SDescribeBuilder());
    divide_builder->setMethod("Divide");
    divide_builder->setParamDescribe("num1", myrpc::server::VType::INTEGRAL);
    divide_builder->setParamDescribe("num2", myrpc::server::VType::INTEGRAL);
    divide_builder->setResultType(myrpc::server::VType::INTEGRAL);
    divide_builder->setServiceCallBack(Divide);

    // 注册乘法服务
    std::unique_ptr<myrpc::server::SDescribeBuilder> multiply_builder(new myrpc::server::SDescribeBuilder());
    multiply_builder->setMethod("Multiply");
    multiply_builder->setParamDescribe("num1", myrpc::server::VType::INTEGRAL);
    multiply_builder->setParamDescribe("num2", myrpc::server::VType::INTEGRAL);
    multiply_builder->setResultType(myrpc::server::VType::INTEGRAL);
    multiply_builder->setServiceCallBack(Multiply);

    // 创建Rpc调用服务器 并开启服务注册功能
    myrpc::Address access_addr = std::make_pair("127.0.0.1", 8005);
    myrpc::Address registry_server_addr = std::make_pair("127.0.0.1", 28080);
    auto server = std::make_shared<myrpc::server::RpcServer>(access_addr, true, registry_server_addr);
    
    // 注册多个服务方法
    server->registryMethod(divide_builder->build());
    server->registryMethod(multiply_builder->build());

    server->start();

    mylog::shutdown();
    return 0;
}
